package com.tycmc.storm.bolt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.tycmc.kafka.CreateKafkaProducer;
import com.tycmc.common.log.AppendLog;
import com.tycmc.rabbitmq.CreateRabbitMQConnection;
import com.tycmc.common.util.PrintException;
import com.tycmc.common.util.Property;
import com.tycmc.system.AppContext;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple;

import java.util.Map;

public class ResultOutputBolt extends BaseRichBolt {
    static {
        AppContext.getSpringContext();
    }

    private OutputCollector collector;
    /**
     * kafka生产者（保存数据）
     */
    private KafkaProducer<String, String> dataProducer;
    /**
     * kafka生产者（日志信息生产者）
     */
    private KafkaProducer<String, String> logProducer;
    private Channel channel;
    private static AppendLog appendLog = new AppendLog();

    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
        collector = outputCollector;
        dataProducer = new CreateKafkaProducer().initKafkaStorage();
        logProducer = new CreateKafkaProducer().initDaily();
       // channel = CreateRabbitMQConnection.getChannel();
    }

    @Override
    public void execute(Tuple tuple) {
        try {
            String resultJson = (String) tuple.getValueByField("resultJson");
            JSONArray jsonArray = JSON.parseArray(resultJson);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                JSONArray topicList = jsonObject.getJSONArray("topicList");
                String valueList = jsonObject.getString("valueList");
                for (int j = 0; j < topicList.size(); j++) {
                    JSONObject mqConfig = topicList.getJSONObject(j);
                    String mqType = mqConfig.getString("use");
                    if ("kafka".equals(mqType)) {
                        dataProducer.send(new ProducerRecord<>(mqConfig.getString("topic"), valueList));
                    } /*else if ("rabbitmq".equals(mqType)) {
                        channel.basicPublish(mqConfig.getString("exchange"), mqConfig.getString("routingkey"), MessageProperties.PERSISTENT_TEXT_PLAIN, valueList.getBytes("UTF-8"));
                    }*/
                }
            }
        } catch (Exception e) {
            String stackTrace = PrintException.getStackTrace(e);
            logProducer.send(new ProducerRecord<>(Property.getProperty("producerDaily.topic"), appendLog.appendDaily("0", System.currentTimeMillis(), "ResultOutputBolt.execute:" + stackTrace)));
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {

    }
}
